
#ifndef __CMutex_H__
#define __CMutex_H__

#ifndef WIN32

#include <assert.h>

#include <pthread.h>
#include <sys/time.h> 
#include <unistd.h>
#include <sys/timeb.h> 

#include <sys/sem.h>

#include <stdlib.h>
#include <time.h>

#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> 

#include <sys/ipc.h>
#include <sys/shm.h>
   
#include <sys/wait.h>
#include <signal.h>

#include <errno.h>
#include <sys/types.h>

#include <sys/time.h>
#include <errno.h>
#include <bits/pthreadtypes.h>
 
 //线程间锁
class CMutex  // 
{
public:

    CMutex( )  
    { 
        pthread_mutexattr_t* muterattr=NULL;
        pthread_mutex_init(&mutex, muterattr);
  
    }

    virtual ~CMutex()      
    { 
         pthread_mutex_destroy(&mutex); 
    }

    int Lock( )     
    { 
        pthread_mutex_lock(&mutex); 
    }

    void Unlock()   
    { 
        pthread_mutex_unlock(&mutex); 
    }

    bool tryLock()     
    { 
        return (pthread_mutex_trylock(&mutex)==0); 
    }
 

private: 
 
    pthread_mutex_t mutex; 
};
 
  
/*
///线程间读写锁
class CRWLock
{
public: 
	CRWLock()
	{
		int res = pthread_rwlock_init(&rwlock,NULL);
		if( res != 0 )
		{
			printf("****************   create rwlock error!*********************\n");
			return;
		} 
	}

	~CRWLock()
	{
		pthread_rwlock_destroy(&rwlock);
	}

	void RLock()
	{
		pthread_rwlock_rdlock(&rwlock);
	}
	void WLock()
	{
		pthread_rwlock_wrlock(&rwlock);
	}
	void ULock()
	{
		pthread_rwlock_unlock(&rwlock);
	}

 
private:
	pthread_rwlock_t  rwlock; // 读写锁对象
};

 


class CMutexHelper
{
private:
	CMutShare * m_p ;
	 
public:
	CMutexHelper( CMutShare * p ) { //允许 p为空
		m_p = p ; 
	}

	~CMutexHelper( ){ 
		m_p->Unlock() ; 
	}


	void Lock() { // if is readlocked, auto-unlock, then writelock
 		if( m_p ){
			 m_p->Lock();
		}
	}
 

	void Unlock(){
		if( m_p ){
			 m_p->Unlock();
		} 
	}
};*/

#endif

#endif

